/**
 * Created by json on 2016/8/18.
 */
dwc.LoadingScene = cc.Scene.extend({
    sprite: null,
    ctor: function () {
        this._super();

        this.mainNode=ccs.load(dwc.g_arrRes.loading_scene_json).node;
        this.addChild(this.mainNode);

        this.jindutiao=$.getNodeByName(this.mainNode,"ID_SLIDER_JINDU");
        this.jindutiao.setPercent(0);
        this.jindutiao.setVisible(false);

        var logo = $.getNodeByName(this.mainNode,"ID_SPRITE_LOGO");
        var bundle = $.getBundleID();

        var self = this;
        this.jindutiao.setPercent(0);
        this._assets = new $.Assets("src/project.manifest","update", function (type,msg) {
            self.OnLoadGameResource();
        },self, function (percent) {
            self.jindutiao.setVisible(true);
            self.OnPercent(percent*0.8);
        });
    },
    OnPercent: function (percent) {
        this.jindutiao.setPercent(percent);
    },
    OnLoadGameResource: function () {
        var that=this;
        var l_strBaseDir = "src/gc_src";
        var objJsList = eval("dwc.g_arrGCJSList");
        if(objJsList){
            that.OnLoadPlist();
        }
        this.jindutiao.setPercent(80);
        cc.loader.loadJs(l_strBaseDir, ["gc_js_list.js"], function(){
            cc.loader.loadJs(l_strBaseDir, dwc.g_arrGCJSList, function() {
                cc.loader.load(dwc.g_arrGCResList, function () {
                    that.jindutiao.setPercent(90);
                    for(var i=0;i<dwc.g_arrGCPlist.length;i++){
                        var tmp = eval(dwc.g_arrGCPlist[i]);
                        if(tmp){
                            cc.spriteFrameCache.addSpriteFrames(tmp);
                        }
                    }
                    that.jindutiao.setPercent(100);
                    cc.director.runScene(new dwc.LoginScene());
                }, that);
            });
        });
    },
    OnLoadPlist: function () {

    }
});

//热更

//热更新事件类型
//热更新事件类型
$.ASSETS_EventType = {
    ASSETS_UPADTE_SUCCESS : "GX_ASSETS_UPADTE_SUCCESS",//更新成功
    ASSETS_UPADTE_FAILED  : "GX_ASSETS_UPADTE_FAILED",//更新失败
    ASSETS_UPADTE_PERCENT : "GX_ASSETS_UPADTE_PERCENT", //更新进度
    ASSETS_UPADTE_SKIP : "GX_ASSETS_UPADTE_PERCENT" //跳过
}
$.MAX_FAILED_COUNT = 3;
//共享的下载器
$.Assets = cc.Class.extend({
    m_nfailCount : 0,//记录失败次数
    m_strMainFests : null,
    m_strStoragePaths : null,//存储路径
    //isNoStartImmediately undefined时 立马Start
    ctor : function(__mainfest, __storagePaths,cb,target,progress ,isNoStartImmediately){
        this.m_nfailCount = 0;//记录失败次数
        this.m_strMainFests = __mainfest;
        this.m_strStoragePaths = __storagePaths;//存储路径

        this.cb = cb;
        this.target = target;
        this.progress = progress;
        if (isNoStartImmediately == undefined){
            this.Start();
        }
    },

    Start : function () {

        //是浏览器
        if (!cc.sys.isNative) { //true in jsb, false in browsers
            this.SendEvent($.ASSETS_EventType.ASSETS_UPADTE_FAILED, "This sys is not Native! 热更新只支持JSB.");
            return;
        }
        //存储路径
        var l_strStoragePath = ((jsb.fileUtils ? jsb.fileUtils.getWritablePath() : "/") + this.m_strStoragePaths);

        this.m_AssetsManager = new jsb.AssetsManager(this.m_strMainFests, l_strStoragePath);
        this.m_AssetsManager.retain();

        //加载配置文件失败
        if (!this.m_AssetsManager.getLocalManifest().isLoaded()) {
            this.SendEvent($.ASSETS_EventType.ASSETS_UPADTE_FAILED, "loaded local project.manifest file failed!");
            this.m_AssetsManager.release();
            return;
        }
        else//加载配置文件成功
        {
            var that = this;
            var listener = new jsb.EventListenerAssetsManager(that.m_AssetsManager, function (event) {
                if(event.getAssetsManagerEx() != that.m_AssetsManager){
                    cc.log("======== not myself");
                    return;
                }
                switch (event.getEventCode()) {
                    case jsb.EventAssetsManager.ERROR_NO_LOCAL_MANIFEST://本地mainfest配置文件未找到
                        //释放下载器
                        that.m_AssetsManager.release();
                        that.SendEvent($.ASSETS_EventType.ASSETS_UPADTE_FAILED, "No local manifest file found!");
                        break;
                    case jsb.EventAssetsManager.UPDATE_PROGRESSION://更新进度中
                        // that._percent = event.getPercent();//获取进度比例
                        // that._percentByFile = event.getPercentByFile();//获取进度文件文件
                        // cc.log(that._percent + "%");
                        var msg = event.getMessage();
                        if (msg) {
                            cc.log(msg);
                        }
                        that.SendEvent($.ASSETS_EventType.ASSETS_UPADTE_PERCENT, event.getPercent());
                        break;
                    case jsb.EventAssetsManager.ERROR_DOWNLOAD_MANIFEST:
                    case jsb.EventAssetsManager.ERROR_PARSE_MANIFEST:
                        //释放下载器
                        that.m_AssetsManager.release();
                        that.SendEvent($.ASSETS_EventType.ASSETS_UPADTE_FAILED, "Fail to download or parse manifest file!");
                        break;
                    case jsb.EventAssetsManager.ALREADY_UP_TO_DATE://更新完成
                    case jsb.EventAssetsManager.UPDATE_FINISHED://更新完成

                        //释放下载器
                        that.m_AssetsManager.release();
                        that.SendEvent($.ASSETS_EventType.ASSETS_UPADTE_SUCCESS, "update success!");
                        break;
                    case jsb.EventAssetsManager.UPDATE_FAILED://更新失败
                        //失败次数统计
                        that.m_nfailCount++;
                        if (that.m_nfailCount < $.MAX_FAILED_COUNT) {
                            that.m_AssetsManager.downloadFailedAssets();
                        }
                        else {
                            //释放下载器
                            that.m_AssetsManager.release();
                            //超过重试次数
                            that.m_nfailCount = 0;//失败次数归零
                            that.SendEvent($.ASSETS_EventType.ASSETS_UPADTE_FAILED, "Reach maximum fail count, exit update process!");
                        }
                        break;
                    case jsb.EventAssetsManager.ERROR_UPDATING://更新失败
                        that.m_AssetsManager.release();
                        that.SendEvent($.ASSETS_EventType.ASSETS_UPADTE_FAILED, "Asset update error: " + event.getAssetId() + ", " + event.getMessage());
                        break;
                    case jsb.EventAssetsManager.ERROR_DECOMPRESS://错误的解压缩
                        that.m_AssetsManager.release();
                        that.SendEvent($.ASSETS_EventType.ASSETS_UPADTE_FAILED, event.getMessage());
                        break;
                    case jsb.EventAssetsManager.ASSET_UPDATED:
                        break;
                    case jsb.EventAssetsManager.NEW_VERSION_FOUND:
                        break;

                    default:
                        that.m_AssetsManager.release();
                        //cc.assert(0, "");
                        break;
                }
            });
            cc.eventManager.addListener(listener, 1);

            that.m_AssetsManager.update();

        }
    },

    SendEvent : function (__type, __msg)
    {
        var self = this;
        if ($.ASSETS_EventType.ASSETS_UPADTE_PERCENT == __type){
            self.progress.call(self.target,__msg);
        }else{
            self.cb.call(self.target,__type,__msg);
        }
    }
});

